package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Lavora;
import dbms.Postgres;

/**
 * Servlet implementation class AllJob
 */
@WebServlet("/AllJob")
public class AllJob extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AllJob() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Postgres p = Postgres.getInstance();
		int id = Integer.parseInt(request.getParameter("id"));
		ResultSet rs = p.getAllJob(id);
		Lavora l;
		List list = new ArrayList();
		try {
			while (rs.next()) {
				l = new Lavora();
				l.setId(Integer.parseInt(rs.getString("id_lavora")));
				l.setId_persona(Integer.parseInt(rs.getString("id_persona")));
				l.setId_occupazione(rs.getString("id_occupazione"));
				l.setData_inizio(rs.getString("data_inizio"));
				l.setData_fine(rs.getString("data_fine"));
				l.setDescrizione(rs.getString("descrizione"));
				list.add(l);

			}
			request.getSession().setAttribute("occupazioni", list);
			response.sendRedirect("VisJob.jsp");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
